/**
 * 添加或者修改页面
 */
var TExhaustEquipmentInfoDlg = {
    data: {
        companyId:"",
        exhaustName: "",
        exhaustNum: "",
        buildTime: "",
        operatingExpenses: "",
        operatingTime: "",
        firstOperating: "",
        secondOperating: "",
        thirdOperating: "",
        fourthOperating: "",
        craft: ""

    }
};

layui.use(['form', 'admin','laydate','upload', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var upload = layui.upload;

    //让当前iframe弹层高度适应
    //admin.iframeAuto();

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/tExhaustEquipment/addItem", function (data) {
            if (data.code == 200) {
                Feng.success("添加成功！");
            }else{
                Feng.error("添加失败！" + data.message)
            }

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.message)
        });
        ajax.set(data.field);
        data.field.equipmentPhoto = $('#equipmentPhoto').val();
        ajax.start();

        return false;
    });


    // 渲染时间选择框
    laydate.render({
        elem: '#buildTime'
    });

    /*监听公司名称*/
    var ajax1 = new $ax(Feng.ctxPath + "/tBasicinfo/basicList");
    var companyList = ajax1.start();
    console.log(companyList);
    for (var i = 0 ; i < companyList.length ; i++){
        $("#companyId").append("<option value=\"" + companyList[i].id + "\">" + companyList[i].companyName + "</option>");
    }
    //重新渲染
    layui.form.render("select");



    /*多图上传*/
    var uploadInst  = upload.render({
        elem: '#btn_multiple_upload_img'
        ,url:  Feng.ctxPath+'/ali/uploadFile' //改成您自己的上传接口
        ,multiple: true
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#upload_image_list').append('<img id = "shabi" style="height: 66px;margin-left: 7px" src="'+ result +'   " title="单击删除" οnclick="delMultipleImgs(this)" class="layui-upload-img">')
            });
        }
        ,done: function(res){
            //上传完毕
            if (res.code== 0) {

                var last_url = $(".upload_image_url").val();
                var upload_image_url = "";
                if(last_url){

                    upload_image_url = last_url+","+res.data;
                    $("#equipmentPhoto").val(upload_image_url);

                }else {
                    upload_image_url = res.data;
                    $("#equipmentPhoto").val( upload_image_url);
                }
                $(".upload_image_url").val(upload_image_url);



            }
        }
    });


    /**
     * 多图清除按钮点击事件
     */
    $("#btn_image_clear").click(function () {
        $('#upload_image_list').html("");
        $(".upload_image_url").val('');
    });
    i
    function delMultipleImgs(this_img) {
        //获取下标
        var subscript = $("#upload_image_list img").index(this_img);
        multiple_images = $('.upload_image_url').val().split(",");
        //删除图片
        this_img.remove();
        //删除数组
        multiple_images.splice(subscript, 1);
        $('.upload_image_url').val(multiple_images);
    };



    $('#upload_image_list').on('click', $('shabi'), function(data){
        var subscript = $("#upload_image_list img").index(data.target);
        multiple_images = $('.upload_image_url').val().split(",");
        console.log(multiple_images,"////////////////////")
        //删除图片
        data.target.remove();
        //删除数组
        multiple_images.splice(subscript, 1);
        $('.upload_image_url').val(multiple_images);

    })
});